package tw.chen.unlock;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class TransparentActivity extends Activity 
{
	private String TAG = this.getClass().getName();
	
	int AXIS, POS_LIMIT, NEG_LIMIT, DRIFT, DIRECT;
	
	float[] acc_value = new float[3];
	float[] mag_value = new float[3];
	
	SensorManager sManager;
	Sensor ACC, MAG;
	
	Window window;
	
	IntentFilter filter;
	
	SharedPreferences setting;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transparent);
		
		Log.i(TAG, "transparent activity start...");
		
		setting = getSharedPreferences(Values.SETTING, Values.SETTING_PAGE);
		
		AXIS = setting.getInt(Values.AXIS_TYPE, Values.AXIS_X);
		DIRECT = setting.getInt(Values.DIRECTION, 0);
		DRIFT = setting.getInt(Values.DRIFT, 10);
		
		int value = setting.getInt(Values.DEGREES, 0);
		if (value != 0)
		{
			POS_LIMIT = value;
			NEG_LIMIT = 0 - value;
		}
		
		filter = new IntentFilter();
		filter.addAction(Intent.ACTION_USER_PRESENT);
		registerReceiver(mReceiver, filter);
		
		sManager = (SensorManager)getSystemService(SENSOR_SERVICE);
		window = getWindow();
		
		ACC = sManager.getDefaultSensor(Values.ACCELEROMETER);
		MAG = sManager.getDefaultSensor(Values.MAGNETIC);
		
		if (AXIS == Values.AXIS_Z)
		{
			sManager.registerListener(mListener, ACC, Values.rateUs_FAST);
			sManager.registerListener(mListener, MAG, Values.rateUs_FAST);
		}
		else
		{
			sManager.registerListener(mListener, ACC, Values.rateUs_NORMAL);
			sManager.registerListener(mListener, MAG, Values.rateUs_NORMAL);
		}
	}
	
	private SensorEventListener mListener = new SensorEventListener()
	{
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {}

		@Override
		public void onSensorChanged(SensorEvent event) 
		{
			switch (event.sensor.getType())
			{
			case Sensor.TYPE_ACCELEROMETER:
				acc_value = event.values;
				break;
			case Sensor.TYPE_MAGNETIC_FIELD:
				mag_value = event.values;
				break;
			}
			
			calculateDegrees();
		}
	};
	private void calculateDegrees()
	{
		float[] mValues = new float[3];
		float[] mR = new float[9];
		
		SensorManager.getRotationMatrix(mR, null, acc_value, mag_value);
		SensorManager.getOrientation(mR, mValues);
		
		for (int i = 0; i < mValues.length; i++)
		{
			mValues[i] = (float)Math.toDegrees(mValues[i]);
		}
		
		int x = (int)mValues[1];
		int y = (int)mValues[2];
		int z = (int)mValues[0];
		if (z < 0)
			z = 180 + (180 - Math.abs(z));
		
		switch (AXIS)
		{
		case Values.AXIS_X:
			if ((x > POS_LIMIT) || (x < NEG_LIMIT))
				dissable();
			break;
		case Values.AXIS_Y:
			if ((y > POS_LIMIT) || (y < NEG_LIMIT))
				dissable();
			break;
		case Values.AXIS_Z:
			switch (DIRECT)
			{
			case Values.EAST:
				if ((z > 90 - DRIFT) && (z < 90 + DRIFT))
					dissable();
				break;
			case Values.EAST_S:
				if ((z > 135 - DRIFT) && (z < 135 + DRIFT))
					dissable();
				break;
			case Values.SOUTH:
				if ((z > 180 - DRIFT) && (z < 180 + DRIFT))
					dissable();
				break;
			case Values.SOUTH_W:
				if ((z > 225 - DRIFT) && (z < 2252 + DRIFT))
					dissable();
				break;
			case Values.WEST:
				if ((z > 270 - DRIFT) && (z < 270 + DRIFT))
					dissable();
				break;
			case Values.WEST_N:
				if ((z > 315 - DRIFT) && (z < 315 + DRIFT))
					dissable();
				break;
			case Values.NORTH:
				if ((z > 360 - DRIFT) || (z < DRIFT))
					dissable();
				break;
			case Values.NORTH_E:
				if ((z > 45 - DRIFT) && (z < 45 + DRIFT))
					dissable();
				break;
			}
			break;
		}
	}
	
	private void dissable()
	{
		window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
		finish();
	}
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver()
	{
		@Override
		public void onReceive(Context arg0, Intent intent) 
		{
			String action = intent.getAction();
			
			if (action.equals(Intent.ACTION_USER_PRESENT))
				finish();
		}
	};
	
	@Override
	protected void onDestroy() 
	{
		unregisterReceiver(mReceiver);
		sManager.unregisterListener(mListener);
		super.onDestroy();
	}
}
